$(function() {
    var roleId = $("#con_roleId").val(); //3:fse; 4:tl; 5:bu;
    var fseId = $("#con_fseId").val(); //3:fse; 4:tl; 5:bu;
    var tlId = $("#con_tlId").val(); //3:fse; 4:tl; 5:bu;
    if (roleId == 3){//fse
        $("#buName").hide();
        $("#teamleaderName").hide();
        $("#enginnerName").hide();
        $("#goSearch").hide();

        $("#enginnerId").val(fseId);
    } else if (roleId == 4){//tl
        $("#buName").hide();
        $("#teamleaderName").hide();
        $("#enginnerName").show();

        $("#teamleaderId").val(tlId);
        enginnerInit();
    } else {//bu
        $("#buName").show();
        $("#teamleaderName").show();
        $("#enginnerName").show();
        buInit();
        teamleaderInit();
        enginnerInit();
    }
})

function buChange() {
    var buName = $("#buName").val();
    var buStr = $("#buStr").val();
    if (buStr.indexOf(buName) < 0) {
        $("#bu").val("");
    }
    teamleaderInit();
    enginnerInit();
}

function teamleaderChange() {
    var teamleaderName = $("#teamleaderName").val();
    var teamleaderStr = $("#teamleaderStr").val();
    if (teamleaderStr.indexOf(teamleaderName) < 0) {
        $("#teamleaderId").val("");
    }
    enginnerInit();
}

function buInit() {
    $.ajax({
        "url": ctx + "system/user/selectBuList",
        "type": "post",
        "async": false,
        "dataType": "json",
        // "beforeSend": function () {
        //     $.modal.loading("正在处理中，请稍后...");
        // },
        "success": function (result) {
            
            var data = [];
            var buStr = "";
            if (result.data != null && result.data != "undefined")
                for (var i = 0; i < result.data.length; i++) {
                    data.push({
                        "key":result.data[i].bu,
                        "value":result.data[i].bu
                    });
                    buStr += result.data[i].name + ", ";
                }
            // console.log(buStr)
            $("#buStr").val(buStr);
            $("#buName").typeahead({
                source: data,
                displayText: function (item) {
                    return item.value;//返回字符串
                },
                afterSelect: function (item) {
                    $("#buId").val(item.key);
                }
            });
        }
    });
    
}

function teamleaderInit() {
    $.ajax({
        "url": ctx + "system/user/selectTeamleaderList",
        "data" : {
            "bu" : $("#buName").val()
        },
        "type": "post",
        "async": false,
        "dataType": "json",
        "success": function (result) {
            
            var data = [];
            var teamleaderStr = "";
            if (result.data != null && result.data != "undefined")
                for (var i = 0; i < result.data.length; i++) {
                    data.push({
                        "key":result.data[i].teamleaderId,
                        "value":result.data[i].userName
                    });
                    teamleaderStr += result.data[i].userName + ", ";
                }
            // console.log(teamleaderStr)
            $("#teamleaderStr").val(teamleaderStr);
            $("#teamleaderName").typeahead({
                source: data,
                displayText: function (item) {
                    return item.value;//返回字符串
                },
                afterSelect: function (item) {
                    $("#teamleaderId").val(item.key);
                }
            });
        }
    });
    
}

function enginnerInit() {
    $.ajax({
        "url": ctx + "system/user/selectEnginnerList",
        "data" : {
            "bu" : $("#buName").val(),
            "teamleaderId" : $("#teamleaderId").val()
        },
        "type": "post",
        "async": false,
        "dataType": "json",
        "success": function (result) {
            
            var data = [];
            var enginnerStr = "";
            if (result.data != null && result.data != "undefined")
                for (var i = 0; i < result.data.length; i++) {
                    data.push({
                        "key":result.data[i].enginnerId,
                        "value":result.data[i].userName
                    });
                    enginnerStr += result.data[i].userName + ", ";
                }
            // console.log(enginnerStr)
            $("#enginnerStr").val(enginnerStr);
            $("#enginnerName").typeahead({
                source: data,
                displayText: function (item) {
                    return item.value;//返回字符串
                },
                afterSelect: function (item) {
                    // alert(item.key);
                    $("#enginnerId").val(item.key);
                }
            });
        }
    });
    
}
